﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace Util
{
    [Serializable]
    public class PersistentObject
    {
        public void Serialize<T>(string path)
        {
            Stream stream = File.Open(path, FileMode.Create);
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            binaryFormatter.Serialize(stream, this);
            stream.Close();
        }

        public static void Serialize<T>(string path, T instance)
        {
            Stream stream = File.Open(path, FileMode.Create);
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            binaryFormatter.Serialize(stream, instance);
            stream.Close();
        }

        public static T DeSerialize<T>(string path)
        {
            T instance;
            Stream stream = File.Open(path, FileMode.Open);
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            instance = (T)binaryFormatter.Deserialize(stream);
            stream.Close();
            return instance;
        }
    }
}
